package com.pe1n.yangtaopai.view.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyAmountRecylerAdapter;
import com.pe1n.yangtaopai.base.BaseFragment;
import com.pe1n.yangtaopai.bean.ListBean;
import com.pe1n.yangtaopai.bean.ListfourBean;
import com.pe1n.yangtaopai.bean.ListthreeBean;
import com.pe1n.yangtaopai.bean.ListtwoBean;
import com.pe1n.yangtaopai.presenter.PreaenterImpl;
import com.pe1n.yangtaopai.utils.URLConstant;
import com.pe1n.yangtaopai.view.interfacese.IView;

import java.util.ArrayList;
import java.util.List;


public class AmountFragment extends BaseFragment implements IView{
    private Context mContext;
    private XRecyclerView xRecyclerView;
    private MyAmountRecylerAdapter myAmountRecylerAdapter;
    private int count=1;
    private PreaenterImpl mPreaenterImpl;
    private List<Object> myBeanList;
    private ProgressBar progressBar;
    private ImageView imageView;//控制侧滑菜单
    private DrawerLayout  drawerLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_amount, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.xrccyclerview_amount);
        progressBar= (ProgressBar) view.findViewById(R.id.progressbar_amount_fragment);
        imageView= (ImageView) view.findViewById(R.id.imageview_drawerlayout_amount);
        drawerLayout= (DrawerLayout) view.findViewById(R.id.fragment_amount_drawerLayout);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        mPreaenterImpl = new PreaenterImpl(this);
        mPreaenterImpl.LoadImageDate(URLConstant.LIST_URL);


    }

    private void initView() {
        myBeanList=new ArrayList<>();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!drawerLayout.isDrawerOpen(GravityCompat.START)){
                    drawerLayout.openDrawer(GravityCompat.START);
                }
                else{
                    drawerLayout.closeDrawer(GravityCompat.START);
                }
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, OrientationHelper.VERTICAL, false);
        xRecyclerView.setLayoutManager(layoutManager);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
        myAmountRecylerAdapter = new MyAmountRecylerAdapter(mContext,  myBeanList);
        xRecyclerView.setAdapter(myAmountRecylerAdapter);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                count+=1;
                String string=String.format(URLConstant.LISTFOUR_URL,count);
                mPreaenterImpl.LoadImageFourData(string);

            }
        });

    }

    @Override
    public void showProgress() {

        progressBar.setVisibility(View.GONE);
    }


    @Override
    public void showLoadFailMsg() {

    }
    //第一条
    @Override
    public void loadData(ListBean listBean) {
        //第一条图片
        myBeanList.add(listBean);
        mPreaenterImpl.LoadImageTwoData(URLConstant.LISTTWO_URL);


    }
//第二条
    @Override
    public void LoadImageTwoData(ListtwoBean listtwoBean) {
        myBeanList.add(listtwoBean);
        mPreaenterImpl.LoadImageThreeData(URLConstant.LISTTHREE_URL);

    }
//第三条
    @Override
    public void LoadImageThreeData(ListthreeBean listthreeBean) {
        myBeanList.add(listthreeBean);
       String string=String.format(URLConstant.LISTFOUR_URL,count);
       mPreaenterImpl.LoadImageFourData(string);

    }
//第四条
    @Override
    public void LoadImageFourData(ListfourBean listfourBean) {
        myBeanList.add(listfourBean);
        myAmountRecylerAdapter.notifyDataSetChanged();
        xRecyclerView.loadMoreComplete();


    }
}
